package com.example.lifecycledemo.observer;

import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

/**
 * Created by xiuzhizhu
 * Date: 2020/11/29
 */

public class MyApplicationObserver implements LifecycleObserver {

    private static final String TAG = "MyApplicationObserver";

    // 应用启动创建时调用，在应用的整个生命周期只调用一次
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate(){
        Log.i(TAG, "onCreate: Lifecycle.Event.ON_CREATE");
    }

    // 应用程序在前台可见时调用（此时不能与用户交互）
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart(){
        Log.i(TAG, "onStart: Lifecycle.Event.ON_START");
    }

    // 应用程序在前台与用户可交互时调用
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        Log.i(TAG, "onResume: Lifecycle.Event.ON_RESUME");
    }

    // 应用程序退到后台仍可见时调用（此时不能与用户交互）
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        Log.i(TAG, "onPause: Lifecycle.Event.ON_PAUSE");
    }

    // 应用程序退到后台不可见时调用
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        Log.i(TAG, "onStop: Lifecycle.Event.ON_STOP");
    }

    // 永远不会调用，系统不会分发 ON_DESTROY 事件
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy(){
        Log.i(TAG, "onDestroy: Lifecycle.Event.ON_DESTROY");
    }
}
